//1. 导入vue-router相关函数
import { createRouter, createWebHashHistory } from 'vue-router';
import useStore from '@/store/index';
// import { login, getInfo, yqLogin } from '@/api/login';
// import { storeToRefs } from 'pinia';
// import { getUrlKey } from '@/utils/util';
// import { getInfo } from '@/api/login';
import cache from '@/utils/cache';
/*
* @Description:
* @Author: your name
* @Date: 2023-12-18
* @LastEditTime: 2023-12-18
* @LastEditors: Please set LastEditors

  meta: {
    showNav: 当前页面是否显示公共navbar
    title: 当前页面navbar 标题
    backUrl: 当前页面返回路径
  }
*/

// 2.路由规则
const routes = [
  {
    path: '/',
    name: 'login',
    meta: {
      showNav: false
    },
    component: () => import('@/views/login'),
    beforeEnter: async (to, from, next) => {
      const { user } = useStore();
      if (cache.local.getTokenloc()) {
        await user.getInfo();
        if (from.path !== '/' && from.name) {
          next(from.path);
        } else if (to.name === 'login') {
          next('/home');
        } else {
          next();
        }
      } else {
        next()
      }
    }
  },

  {
    path: '/home',
    name: 'home',
    meta: {
      showNav: false,
      title: '整经MES'
    },
    component: () => import('@/views/home/index')
  },
  {
    path: '/wglssTask',
    name: 'wglssTask',
    meta: {
      showNav: true,
      title: '1楼外购料上送',
      backUrl: '/home'
    },
    component: () => import('@/views/wglssTask/index')
  },
  {
    path: '/inventory',
    name: 'inventory',
    meta: {
      showNav: true,
      title: '实时库存',
      backUrl: '/home'
    },
    component: () => import('@/views/inventory/index')
  },
  {
    path: '/inventoryDetail',
    name: 'inventoryDetail',
    meta: {
      showNav: true,
      title: '库存详情',
      backUrl: 'parent'
    },
    component: () => import('@/views/inventory/detail')
  }
];
// 3.路由对象实例化
const router = createRouter({
  history: createWebHashHistory(),
  routes
});
// 暴露导出
export default router;
